<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">简图</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <a-dropdown>
            <a-space size="large">
              <a-avatar :src="userStore().loginUserInfo.avatar" />
              {{ userStore().loginUserInfo.name ?? '无名' }}
            </a-space>
            <template #overlay>
              <a-menu>
                <a-menu-item :onclick="userStore().userLogout">
                   <ApartmentOutlined/>
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { HomeOutlined, ApartmentOutlined } from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { userStore } from '@/stores/user.ts'

const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/manager',
    icon: () => h(ApartmentOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/about',
    icon: () => h(ApartmentOutlined),
    label: '关于',
    title: '关于',
  },
])

const router = useRouter()
// 当前要高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化，更新高亮菜单项
router.afterEach((to) => {
  current.value = [to.path]
})

// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

const logoutClick = () => {
  userStore().userLogout()
}
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}

#globalHeader .user-login-status {
  height: 50px;
  width: 170px;
}
</style>
